import java.util.HashMap;
import java.util.Map;

/**
 * Created by loso on 2017/3/17.
 */
public class _290_WordPattern {


    public boolean wordPatternBigBoss(String pattern, String str) {
        String[] words = str.split(" ");
        if (words.length != pattern.length())
            return false;
        Map index = new HashMap();
        for (Integer i=0; i<words.length; ++i)
            if (index.put(pattern.charAt(i), i) != index.put(words[i], i))
                return false;
        return true;
    }

    public boolean wordPattern(String pattern, String str) {
        if (pattern.length() != str.split(" ").length){
            return false;
        }
        int[] mode1 = getMode(pattern);
        int[] mode2 = getWorldMode(str);
        return judgeMode(mode1,mode2);
    }

    public boolean judgeMode(int[] mode1, int[] mode2){
        for (int i = 0; i < mode1.length; i++) {
            if (mode1[i] != mode2[i]){
                return false;
            }
        }
        return true;
    }

    public int[] getWorldMode(String s){
        int count = 0;
        String[] slist = s.split(" ");
        Map<String,Integer> map = new HashMap<>();
        for (int i = 0; i < slist.length; i++) {
            if (!map.containsKey(slist[i])){
                map.put(slist[i],count++);
            }
        }
        int[] req = new int[slist.length];
        for (int i = 0; i < slist.length; i++) {
            req[i] = map.get(slist[i]);
        }
        return req;
    }

    public int[] getMode(String s){
        int count = 0;
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i))){
                map.put(s.charAt(i),count++);
            }
        }

        int[] req = new int[s.length()];
        for (int i = 0; i < s.length(); i++) {
            req[i] = map.get(s.charAt(i));
        }
        return req;
    }
}
